#!/bin/sh
# - oltp_tables_count
# - oltp_table_size
# - nr_threads
# - rand_type
# - rand_spec_pct
# - runtime
# - report_interval
# - oltp_read_only
# - events
# - _mysql_user
# - _mysql_host
# - _mysql_port
# - _mysql_db
# - _mysql_password
# - _db_driver
# - _mysql_socket
# - _server

: "${_mysql_user:=root}"
: "${_mysql_host:=$_server}"
: "${_mysql_port:=3306}"
: "${_mysql_db:=sysbench_test}"
: "${_mysql_password:=_mysql_password}"
: "${_db_driver:=mysql}"
: "${oltp_tables_count:=1000}"
: "${oltp_table_size:=100000}"
: "${rand_type:=special}"
: "${rand_spec_pct:=100}"
: "${nr_threads:=256}"
: "${runtime:=7200}"
: "${report_interval:=1}"
: "${oltp_read_only:=off}"
: "${events:=2000000000}"
: "${_mysql_socket:=/data/mysql/run/mysql.sock}"

args1=(
	 --mysql-user=$_mysql_user
	 --mysql-host=$_mysql_host
 	 --mysql-port=$_mysql_port
 	 --mysql-db=$_mysql_db
	 --db-driver=$_db_driver
	 --mysql-password=$_mysql_password
	 --table-size=$oltp_table_size
	 --tables=$oltp_tables_count
	 --rand-type=$rand_type
	 --rand-spec-pct=$rand_spec_pct
	 --threads=$nr_threads
	 --time=$runtime
	 --mysql-socket=$_mysql_socket
)

args2=(
	--mysql-user=$_mysql_user
	--mysql-socket=$_mysql_socket
	--mysql-password=$_mysql_password
	--mysql-host=$_mysql_host
	--mysql-port=$_mysql_port
	--mysql-db=$_mysql_db
	--threads=$nr_threads
	--oltp-read-only=$oltp_read_only
	--oltp-table-size=$oltp_table_size
	--oltp-tables-count=$oltp_tables_count
	--report-interval=$report_interval
	--time=$runtime
	--events=$events
)

stop_firewalld()
{
	systemctl stop firewalld >/dev/null 2>&1
	iptables -F >/dev/null 2>&1
}

run_sysbench_step()
{
	lua_script=$1
	shift
	sysbench /usr/local/share/sysbench/tests/include/oltp_legacy/$lua_script "$@"
}

run_sysbench_mysql()
{
	sysbench "${args1[@]}" oltp_read_write prepare > /dev/null

	run_sysbench_step oltp.lua "${args2[@]}" run

	run_sysbench_step oltp.lua "${args2[@]}" cleanup > /dev/null
}

stop_firewalld
run_sysbench_mysql
